#include "mylogger.h"
#include <QDebug>

using namespace ns_yht;

spdlog::level::level_enum MyLogger::global_level = spdlog::level::info;

std::shared_ptr<spdlog::logger> MyLogger::gLogger;
std::shared_ptr<spdlog::logger> MyLogger::gLogger_ais;
bool MyLogger::developDebug = false;

spdlog::level::level_enum MyLogger::getGlobalLevel()
{
    return global_level;
}

std::vector<spdlog::sink_ptr> MyLogger::createSinks(const std::string &log_file_name)
{
    std::vector<spdlog::sink_ptr> sinks;

    auto sink1 = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    sink1->set_level(MyLogger::getGlobalLevel());
    sink1->set_pattern("[%H:%M:%S] [%l] %v");
    sinks.push_back(sink1);

    auto sink2 = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file_name, 1024 * 1024 * 10, 20, false);
    sink2->set_level(spdlog::level::debug);
    sink2->set_pattern("[%Y-%m-%d %H:%M:%S] [%l] %v");
    sinks.push_back(sink2);
    return sinks;
}

void MyLogger::createLogger(const std::string &dir_name, const std::string &logger_name, bool ais, bool debug)
{
    if (!gLogger)
    {
        std::string log_file_name1 = dir_name + "/" + logger_name + "_log.txt";
        auto sinks1 = MyLogger::createSinks(log_file_name1);

        auto logger1 = std::make_shared<spdlog::logger>(logger_name, begin(sinks1), end(sinks1));
        if (debug)
            logger1->set_level(spdlog::level::debug);
        else
            logger1->set_level(spdlog::level::info);
        spdlog::set_default_logger(logger1); // 未设置默认logger会导致有时无法记录日志到文件
                                             // spdlog::register_logger(logger); //register是为了可以get

        gLogger = logger1;
    }

    if (ais && !gLogger_ais)
    {
        std::string log_file_name2 = dir_name + "/" + logger_name + "_ais.txt";
        // auto sinks = MyLogger::createSinks(log_file_name);
        auto sink2 = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file_name2, 1024 * 1024 * 10, 20, false);
        sink2->set_level(spdlog::level::debug);
        sink2->set_pattern("[%Y-%m-%d %H:%M:%S] [%l] %v");
        auto logger2 = std::make_shared<spdlog::logger>(logger_name, sink2);
        logger2->set_level(spdlog::level::debug);
        // spdlog::register_logger(logger); //再次register会异常
        gLogger_ais = logger2;
    }
}

std::shared_ptr<spdlog::logger> MyLogger::getLogger(const std::string &logger_name)
{
    auto logger = spdlog::get(logger_name);
    if (!logger)
    {
        createLogger("", logger_name);
        logger = spdlog::get(logger_name);
    }
    return logger;
}

void MyLogger::init()
{
    auto level = spdlog::level::debug;
    if (std::getenv("STAGE") != NULL)
    {
        std::string stage = std::getenv("STAGE");
        if (stage == "dev")
            level = spdlog::level::debug;
    }
    MyLogger::global_level = level;

    spdlog::flush_every(std::chrono::seconds(1));
    spdlog::flush_on(spdlog::level::debug);

    spdlog::set_pattern("[%Y-%m-%d %H:%M:%S] [%l] %v");
}

void MyLogger::developInfo(const std::string &info)
{
    if (developDebug)
    {
        GLOG->debug(info);
    }
}